package com.likersai.funa.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 加密相关工具类
 *
 * @author sun.ye
 * @since 2021-02-22 14:49
 */
public class SecurityUtil {

    private static final Base64.Decoder DECODER = Base64.getDecoder();

    private static final Base64.Encoder ENCODER = Base64.getEncoder();

    private static MessageDigest sha1 = null;

    static {
        try {
            sha1 = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static String getShaSecurity(String context) {

        try {
            sha1 = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] sha1Bytes =  sha1.digest(context.getBytes());
        return ENCODER.encodeToString(sha1Bytes);

    }


    public static String getToken(String login){
       return getShaSecurity(login + System.currentTimeMillis());
    }

    public static String decodeBase64(String encode){
        byte[] pwd = Base64.getDecoder().decode(encode);
        return new String(pwd);
    }

}